#include "SpriteBlur.h"
#include <renderer\CCGLProgram.h>
#include <renderer\ccShaders.h>
#include <deprecated\CCString.h>
#include <platform\CCFileUtils.h>

namespace cocos2d
{
	SpriteBlur::~SpriteBlur()
	{
	}

	bool SpriteBlur::initWithTexture(Texture2D* texture, const Rect& rect)
	{
		_blurRadius = 0;
		if( Sprite::initWithTexture(texture, rect) ) 
		{
	#if CC_ENABLE_CACHE_TEXTURE_DATA
			auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
					setGLProgram(nullptr);
					initGLProgram();
				});

			_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
	#endif
        
			initGLProgram();

			return true;
		}

		return false;

	}

	SpriteBlur* SpriteBlur::create(const char* pszFileName)
	{
		SpriteBlur* pRet = new (std::nothrow) SpriteBlur();
		if (pRet && pRet->initWithFile(pszFileName))
		{
			pRet->autorelease();
		}
		else
		{
			CC_SAFE_DELETE(pRet);
		}
    
		return pRet;
	}

	void SpriteBlur::initGLProgram()
	{
		GLchar * fragSource = (GLchar*) __String::createWithContentsOfFile(
                                FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh").c_str())->getCString();  
		auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource);

		auto glProgramState = GLProgramState::getOrCreateWithGLProgram(program);
		setGLProgramState(glProgramState);
    
		auto size = getTexture()->getContentSizeInPixels();
		getGLProgramState()->setUniformVec2("resolution", size);
		getGLProgramState()->setUniformFloat("blurRadius", _blurRadius);
		getGLProgramState()->setUniformFloat("sampleNum", 7.0f);
	}

	void SpriteBlur::setBlurRadius(float radius)
	{
		_blurRadius = radius;
		getGLProgramState()->setUniformFloat("blurRadius", _blurRadius);
	}

	void SpriteBlur::getBlurSampleNum(float num)
	{
		_blurSampleNum = num;
		getGLProgramState()->setUniformFloat("sampleNum", _blurSampleNum);
	}
}